<!DOCTYPE html>
<html class="x-admin-sm">
<head>
    <meta charset="UTF-8">
    <title>宇成建筑</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <link rel="stylesheet" href="../statics/css/font.css">
    <link rel="stylesheet" href="../statics/plugins/layui/css/layui.css">
    <link rel="stylesheet" href="../statics/css/xadmin.css">
    <link rel="stylesheet" href="../statics/css/style.css">
    <style>
        .layui-table-grid-down {
            display: none;
        }
    </style>
</head>

<body class='labelAuto'>
<div class="x-nav">
        <span class="layui-breadcrumb">
            <a href="welcome.html">首页</a>
            <a href="javascript:;">系统设置</a>
            <a>
                <cite>工种施工项管理</cite></a>
        </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
       onclick="location.reload()" title="刷新">
        <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>
    </a>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right;margin-right:10px"
       onclick="window.history.go(-1)" title="后退">
        <i class="iconfontBlod" style="line-height:30px">&#xe74f;</i>
    </a>
</div>
<div class="layui-fluid">
    <div class="layui-d layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body search">
                    <form action="" class="layui-form livingList">
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">搜索</label>
                                <div class="layui-input-block">
                                    <input type="search" autocomplete="off" placeholder="请输入施工项名称" class="layui-input" id='searchInput'>
                                </div>
                            </div>
                            <div class="layui-inline">
                                <button class="layui-btn search" type="button" id='search'>
                                    <i class="iconfont layui-icon">&#xe615;</i>
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="layui-card-body tableCardBody">
                    <table class="layui-table" id="lists" lay-filter="test"></table>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
<script src="../statics/plugins/layui/layui.js" charset="utf-8"></script>
<script src='../statics/plugins/jQuery-3.2.1/jquery-3.2.1.min.js'></script>
<script type="text/javascript" src="../statics/plugins/X-admin/xadmin.js"></script>
<script type="text/javascript" src="../statics/js/public.js?v=1129"></script>
<script type="text/html" id='toolbar'>
    <button class="layui-btn add" auth='addAssignment'>添加</button>
</script>
<script type="text/javascript">
    layui.use(['form', 'layer', "table", "laydate"], function () {
        var table = layui.table;
        var form = layui.form;
        var laydate = layui.laydate;
        var layer = layui.layer;
        var professionId = Number(getQueryString('professionId'));
        $('body').on('click', '.add', function () {
            layer.open({
                type: 1,
                title: "添加施工项",
                area: ['40%', '70%'],
                content: `
                    <form class="layui-form layerOpen" action="">
                        <div class="layui-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">施工项名称</label>
                                <div class="layui-input-block">
                                    <input type="text" class="layui-input" id="name">
                                </div>
                            </div>
                        </div>
                        <div class="layui-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">计 量 单 位</label>
                                <div class="layui-input-block">
                                    <select id='unitId'>
                                        <option value='null'>-</option>
                                    </select>
                                </div>
                            </div>
                        </div>
                        <div class="layui-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">施工项排序</label>
                                <div class="layui-input-block">
                                    <input type='number' id='order' min="1" class='layui-input'>
                                </div>
                            </div>
                        </div>
                        <div class="layui-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">施工项备注</label>
                                <div class="layui-input-block">
                                    <textarea id='remark' class='layui-textarea'></textarea>
                                </div>
                            </div>
                        </div>
                    </form>
                `,
                btn: ['确认', '取消'],
                skin: 'my-skin',
                success: function () {
                    $.ajax({
                        url: url + '/setting/unit/lists',
                        type: "post",
                        async: false,
                        data: {
                            token: getToken()
                        },
                        success: function (r) {
                            if (r.code == 100000) {
                                var option1 = '';
                                for (var i = 0; i < r.data.length; i++) {
                                    option1 += `
                                        <option  value=${r.data[i].id}>${r.data[i].name}</option>
                                    `
                                }
                                $('#unitId').append(option1);
                                form.render();
                            }
                        }
                    })
                    form.render();
                },
                yes: function (index, layero) {
                    var name = $('#name').val();
                    var order = $('#order').val();
                    var remark = $('#remark').val();
                    var unitId = Number($('#unitId option:selected').val());
                    $.ajax({
                        url: url + '/setting/assignment/add',
                        type: 'post',
                        data: {
                            name: name,
                            order: parseInt(order),
                            professionId: Number(professionId),
                            remark: remark,
                            unitId:unitId,
                            token: getToken()
                        },
                        dataType: 'json',
                        success: function (r) {
                            if (r.code == 100000) {
                                tips('添加施工项', r);
                                setTimeout(function () {
                                    table.reload('lists');
                                    layer.closeAll();
                                }, 2000)
                            } else {
                                tips('添加施工项', r);
                            }
                        }
                    })
                }
            })
        })
        var lists = table.render({
            elem: '#lists', //绑定DOM元素
            id: 'lists', //table的id
            title: '审批列表',
            height: 'full-120',
            contentType: "application/json; charset=utf-8",
            url: url + '/setting/assignment/lists', //数据接口地址
            request: {
                pageName: 'page', //页码的参数名称，默认：page
                limitName: 'length' //每页数据量的参数名，默认：limit
            },
            method: 'POST',
            parseData: function (res) {
                return {
                    "code": res.code, //解析接口状态
                    "data": res.data.data,
                    "msg": res.msg,
                    "count": res.data.count
                };
            },
            where: {
                professionId: professionId,
                token: getToken()
            },
            toolbar: '#toolbar',
            defaultToolbar: ['filter'],
            page: true, //开启分页
            limit: 10, //默认每页记录条数
            limits: [10, 20, 50], //每页记录条数可选数量
            cols: [
                [ //表头
                    {field: '', title: '编号', type: 'numbers', width: '8%'},
                    {field: 'name', title: '施工项名称', width: "15%"},
                    {field: 'unitName', title: '计量单位', width: "15%"},
                    {field: 'remark', title: '施工项说明', width: "15%"},
                    {field: 'labelName', title: '标签分类', width: "15%",templet:function(d){
                            return d.labelName==null?'':d.labelName;
                        }},
                    {field: 'order', title: '排序', width: "10%"},
                    {
                        field: '', title: '操作', templet(d) {
                            return `
                            <button class='bianji layui-btn' data-id=${d.id} lay-event='bianji' auth='editAssignment' data-ismajor=${d.isMajor}>编辑</button>
                            <button class='biaoqian layui-btn' data-id=${d.id} lay-event='biaoqian'>标签</button>
                            `
                        }
                    }
                ]
            ],
            response: {
                statusName: 'code', //数据状态的字段名称，默认：code
                statusCode: 100000, //成功的状态码，默认：0
                msgName: 'msg', //状态信息的字段名称，默认：msg
                countName: 'count', //数据总数的字段名称，默认：count
                dataName: 'data' //数据列表的字段名称，默认：data
            },
            done:function(){
                permission();
            }
        });
        table.on('tool(test)', function (data) {
            if (data.event == 'bianji') {
                layer.open({
                    type: 1,
                    title: "编辑",
                    area: ['40%', '60%'],
                    content: `
                        <form class="layui-form layerOpen" action="">
                            <div class="layui-item">
                                <div class="layui-inline">
                                    <label class="layui-form-label">施工项名称</label>
                                    <div class="layui-input-block">
                                        <input type="text" class="layui-input"  value=${data.data.name} id="name">
                                    </div>
                                </div>
                            </div>
                            <div class="layui-item">
                                <div class="layui-inline">
                                    <label class="layui-form-label">计 量 单 位</label>
                                    <div class="layui-input-block">
                                        <select id='unitId'>
                                            <option value='null'>-</option>
                                        </select>
                                    </div>
                                </div>
                            </div>
                            <div class="layui-item">
                                <div class="layui-inline">
                                    <label class="layui-form-label">施工项排序</label>
                                    <div class="layui-input-block">
                                        <input type='number' id='order' min="1" value="${data.data.order}" class='layui-input'>
                                    </div>
                                </div>
                            </div>
                            <div class="layui-item">
                                <div class="layui-inline">
                                    <label class="layui-form-label">施工项备注</label>
                                    <div class="layui-input-block">
                                        <textarea id='remark' class='layui-textarea'>${data.data.remark == null ? "" : data.data.remark}</textarea>
                                    </div>
                                </div>
                            </div>
                        </form>
                    `,
                    btn: ['确认', '取消'],
                    skin: 'my-skin',
                    success: function () {
                        $.ajax({
                            url: url + '/setting/unit/lists',
                            type: "post",
                            async: false,
                            data: {
                                token: getToken()
                            },
                            success: function (r) {
                                if (r.code == 100000) {
                                    var option1 = '';
                                    for (var i = 0; i < r.data.length; i++) {
                                        option1 += `
                                            <option ${r.data[i].name == data.data.unitName ? "selected" : ""} value=${r.data[i].id}>${r.data[i].name}</option>
                                        `
                                    }
                                    $('#unitId').append(option1);
                                    form.render();
                                }
                            }
                        })
                        form.render();
                    },
                    yes: function (index, layero) {
                        var name = $('#name').val();
                        var unitId = Number($('#unitId option:selected').val());
                        var remark = $('#remark').val();
                        var order = parseInt($('#order').val());
                        $.ajax({
                            type: 'post',
                            async: false,
                            url: url + '/setting/assignment/edit',
                            data: {
                                id: data.data.id,
                                unitId: unitId,
                                name: name,
                                remark: remark,
                                order: order,
                                token: getToken()
                            },
                            dataType: "json",
                            success: function (r) {
                                if (r.code == 100000) {
                                    tips('编辑施工项', r);
                                    setTimeout(function () {
                                        layer.closeAll();
                                        table.reload('lists');
                                    }, 2000)
                                } else {
                                    tips('编辑施工项', r);
                                }
                            }
                        })
                    }
                })
            }else if(data.event=='biaoqian'){
                layer.open({
                    type:1,
                    title:"标签",
                    area: ['40%','40%'],
                    content:`
                        <form class="layui-form layerOpen" action="">
                            <div class="layui-item">
                                <div class="layui-inline">
                                    <label class="layui-form-label">标签</label>
                                    <div class="layui-input-block">
                                        <select id='biaoqian' lay-filter="label">
                                            <option value='null'>-</option>
                                        </select>
                                    </div>
                                </div>
                            </div>
                            <div class="layui-item newLabel" style="display: none">
                                <div class="layui-inline">
                                    <label class="layui-form-label">标签</label>
                                    <div class="layui-input-block">
                                        <input type='text' class='layui-input' id='input_biaoqian' value=''>
                                    </div>
                                </div>
                            </div>
                        </form>
                    `,
                    btn: ['确认', '取消'],
                    skin:'my-skin',
                    success:function(){
                        $.ajax({
                            url:url+'/setting/assignment/labelLists',
                            type:'post',
                            contentType:'application/json',
                            data:JSON.stringify({
                                token:getToken()
                            }),
                            success:function(r){
                                if(r.code==100000){
                                    var option='';
                                    for(var i=0;i<r.data.length;i++){
                                        option+=`
                                            <option ${data.data.labelId==r.data[i].id?"selected":""} value=${r.data[i].id}>${r.data[i].name}</option>
                                        `
                                    }
                                    $('#biaoqian').append(option);
                                    form.render();
                                }
                            }
                        })
                        form.render();
                        form.on('select(label)', function(data){
                            if (data.value == 1){
                                $('.newLabel').show();
                            }else{
                                $('.newLabel').hide();
                            }
                        });
                    },
                    yes: function(index, layero){
                        var labelId = $('#biaoqian option:selected').val();
                        if (Number(labelId) == 1){
                            d = {
                                id:Number(data.data.id),
                                labelId:1,
                                name:$('#input_biaoqian').val(),
                                token:getToken()
                            };
                        }else{
                            d = {
                                id:Number(data.data.id),
                                labelId:$('#biaoqian option:selected').val()>0?Number($('#biaoqian option:selected').val()):null,
                                token:getToken()
                            }
                        }
                        $.ajax({
                            type:'post',
                            url:url+'/setting/assignment/label',
                            data:d,
                            dataType: "json",
                            success: function(r) {
                                if(r.code==100000){
                                    tips('标签',r);
                                    setTimeout(function(){
                                        layer.closeAll();
                                        table.reload('lists');
                                    },2000)
                                }else{
                                    tips('标签',r);
                                }
                            }
                        })
                    }
                })
            }
        })
        //搜索
        $('body').on('click', '#search', function () {
            var search = $('#searchInput').val();
            table.reload('lists', {
                page: {
                    curr: 1 //重新从第 1 页开始
                },
                where: {
                    search: search,
                }
            })
        })
    });

</script>

</html>